/**
 * FillupEntryParser.java
 *
 * Copyright (C) 2005-2006 Tommi Laukkanen
 * http://www.substanceofcode.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


package com.substanceofcode.carmanager.model;

/**
 *
 * @author Tommi
 */
public class FillupEntryParser {
    
    /** Creates a new instance of FillupEntryParser */
    private FillupEntryParser() {
    }
    
    /** Parse entry from given record string */
    public static FillupEntry parseEntry(String record) {

        FillupEntry entry = null; 
        
        /** Parse record */
        if(record.startsWith("v2") == true) {
            /** Record version 2 */
        } else {
            /** Record version 1 */
            /** Date|TotalGasLitres|FullFillupFlag|TotalPrice|CurrentMileage|Comment */
            entry = parseVersion1(record);
        }

        /** Return resulted object */
        return entry;        
    }
    
    /** Parse v1 entry */
    public static FillupEntry parseVersion1(String record) {
        /** 
         * Parse information from single record 
         * Example of record content:
         * 31122003|123|0|123|123|comment
         */
        String originalRecordString = record;
        String s = record;
        int i = s.indexOf("|");
        String date = s.substring(0,8);
        s = s.substring(i+1);
        i = s.indexOf("|");
        int totalGasLiters;
        if( s.substring(0, i).length()>0 ) {
            totalGasLiters = Integer.parseInt(s.substring(0, i));
        }
        else {
            totalGasLiters = 0;
        }
        
        s = s.substring(i+1);
        i = s.indexOf("|");
        int fullFillupFlag;
        if( s.substring(0, i).length()>0 ) {
            fullFillupFlag = Integer.parseInt(s.substring(0, i));
        }
        else {
            fullFillupFlag = 0;
        }
        
        s = s.substring(i+1);
        i = s.indexOf("|");
        int totalPrice;
        if( s.substring(0, i).length()>0 ) {
            totalPrice = Integer.parseInt(s.substring(0, i));
        }
        else {
            totalPrice = 0;
        }
        s = s.substring(i+1);
        i = s.indexOf("|");
        int currentMileage;
        if( s.substring(0, i).length()>0 ) {
            currentMileage = Integer.parseInt(s.substring(0, i));
        }
        else {
            currentMileage = 0;
        }
        s = s.substring(i+1);
        String comment = s;        
        
        FillupEntry entry;
        entry = new FillupEntry(date, totalGasLiters, fullFillupFlag, totalPrice, currentMileage, comment, originalRecordString);
        return entry;
    }
    
}
